package com.frkj.yuan.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.frkj.yuan.R;
import com.frkj.yuan.adapter.BaseRecyclerHolder;
import com.frkj.yuan.adapter.BaseRecyclerRefreshAdapter;
import com.frkj.yuan.api.bean.AgainYuyueOrderBean;
import com.frkj.yuan.api.bean.SelectServiceTimeBean;
import com.frkj.yuan.api.category.AgainYuYueOrderCategory;
import com.frkj.yuan.api.presenter.AgainYuYueOrderPresenter;
import com.frkj.yuan.base.BaseActivity;
import com.frkj.yuan.dialog.LoadingDialog;
import com.frkj.yuan.dialog.SelectTimeDialog;
import com.frkj.yuan.fragment.home.activity.YuyueKeShiActivity;
import com.frkj.yuan.fragment.home.activity.YuyueOrderActoivity;
import com.frkj.yuan.main.ConstantStaticUrl;
import com.frkj.yuan.utils.FullScreenUtils;
import com.frkj.yuan.utils.HttpNetworkConnectUtils;
import com.frkj.yuan.utils.SPUtils;
import com.frkj.yuan.utils.ToastUtil;
import com.frkj.yuan.view.RecyclerViewLayoutManager;

import java.io.IOException;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 信息说明:
 * company: 兰州丰荣信息科技有限公司
 * 创建时间: on 2022/4/23 09 ：43
 * 项目名称：yuanban
 */
public class AgainYuYueJiuzhenPersonActivity extends BaseActivity implements AgainYuYueOrderCategory.View {
    @BindView(R.id.base_title)
    TextView mTitleMsg;
    @BindView(R.id.jiuzheng_person)
    TextView mJiuzhengPerson;
    @BindView(R.id.select_yiuan)
    TextView select_yiuan;
    @BindView(R.id.select_ke)
    TextView select_ke;
    @BindView(R.id.select_shijian)
    TextView select_shijian;
    private AgainYuYueOrderPresenter mPresenter;
    private LoadingDialog mpcd;
    private String mToken,mName, mobile,keshiId, mTimeStates, mCarId,mHospitalName, mKeshiName,mLeftTime,selectTime;
    private long patientId,hospitalId,mOrderId;
    private BaseRecyclerRefreshAdapter<SelectServiceTimeBean.SelectServiceTimeBeanData> mLeftAdapter;
    private List<SelectServiceTimeBean.SelectServiceTimeBeanData.PeriodsData> mRightList;
    private BaseRecyclerRefreshAdapter<SelectServiceTimeBean.SelectServiceTimeBeanData.PeriodsData> mRightAdapter;
    @Override
    public int getLayoutId() {
        return R.layout.activity_again_yuyue;
    }
    @Override
    public void initLayoutView() {
        FullScreenUtils.getInstance().fullScreen(AgainYuYueJiuzhenPersonActivity.this,true);
        mOrderId = getIntent().getLongExtra("orderId", 0);
        mTitleMsg.setText("再次预约");
        mPresenter = new AgainYuYueOrderPresenter(this);
        SetPersenter(mPresenter);
        mToken = (String) SPUtils.get(AgainYuYueJiuzhenPersonActivity.this, ConstantStaticUrl.mToken, "");
        mpcd = new LoadingDialog(AgainYuYueJiuzhenPersonActivity.this);
        mpcd.show();
        if (!HttpNetworkConnectUtils.isNetworkConnected(this)) {
            ToastUtil.showToastERROR(AgainYuYueJiuzhenPersonActivity.this, "网络不给力,请稍后重试");
        } else {
            mPresenter.aginOrderMsg(mToken, mOrderId);
        }
    }

    @OnClick({R.id.base_back, R.id.submit_hoptial, R.id.select_person,R.id.select_shijian, R.id.select_yiyuan, R.id.select_keshi})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.base_back:
                finishWitchAnimation();
                break;
            case R.id.select_person:
                Intent person = new Intent(AgainYuYueJiuzhenPersonActivity.this, JiuZhengPesonalActivity.class);
                person.putExtra("person", "person");
                startActivityForResult(person, 100);
                AgainYuYueJiuzhenPersonActivity.this.overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
                break;
            case R.id.submit_hoptial:
                if (select_shijian.getText().toString().trim().equals("")) {
                    ToastUtil.showToastINFO(AgainYuYueJiuzhenPersonActivity.this, "请选择就诊服务时间");
                } else {
                    Bundle bundle = new Bundle();
                    bundle.putString("name", mName);
                    bundle.putString("sfzhm", mCarId);
                    bundle.putString("yiyuan", mHospitalName);
                    bundle.putString("keshi", mKeshiName);
                    bundle.putLong("patientId", patientId);
                    bundle.putLong("hospitalId", hospitalId);
                    bundle.putString("mobile", mobile);
                    bundle.putString("shijian", select_shijian.getText().toString().trim());
                    bundle.putString("keshiId", keshiId);
                    bundle.putString("SelectType", mTimeStates);
                    bundle.putString("selectTime", selectTime);
                    bundle.putLong("mJiuPersonId", patientId);
                    openActivityWitchAnimation(YuyueOrderActoivity.class, bundle);
                }
                break;
            case R.id.select_shijian:
                mPresenter.getSelectTime(hospitalId);
                break;
            case R.id.select_keshi:
                select_shijian.setText("");
                Intent intent1 = new Intent(AgainYuYueJiuzhenPersonActivity.this, YuyueKeShiActivity.class);
                intent1.putExtra("keshi", "keshi");
                intent1.putExtra("hospitalId", hospitalId);
                startActivityForResult(intent1, 500);
                overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
                break;
            case R.id.select_yiyuan:
                select_ke.setText("");
                select_shijian.setText("");
                Intent intent = new Intent(AgainYuYueJiuzhenPersonActivity.this, HospitalListActivity.class);
                intent.putExtra("yiyuan", "yiyuan");
                startActivityForResult(intent, 300);
                overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
                break;
        }
    }
    @Override
    public void aginOrderMsg(AgainYuyueOrderBean orderBean) {
        if (orderBean.getCode() == ConstantStaticUrl.SUCCESS_CODE) {
            mName = orderBean.getData().getPatientName();
            mCarId = orderBean.getData().getIdCard();
            mHospitalName = orderBean.getData().getHospitalName();
            mKeshiName = orderBean.getData().getOfficeName();
            patientId = orderBean.getData().getPatientId();
            hospitalId = orderBean.getData().getHospitalId();
            keshiId = String.valueOf(orderBean.getData().getOfficeId());
            mobile = orderBean.getData().getPatientMobile();
            mJiuzhengPerson.setText(orderBean.getData().getPatientName());
            select_yiuan.setText(orderBean.getData().getHospitalName());
            select_ke.setText(orderBean.getData().getOfficeName());
        } else {
            ToastUtil.showToastINFO(AgainYuYueJiuzhenPersonActivity.this, orderBean.getMsg());
        }
        mpcd.dismiss();
    }

    /**
     * @param serviceTimeBean
     */
    @Override
    public void getSelectTime(SelectServiceTimeBean serviceTimeBean) {
        if (serviceTimeBean.getCode() == ConstantStaticUrl.SUCCESS_CODE) {
            SelectTimeDialog dialog = new SelectTimeDialog(this);
            dialog.show();
            RecyclerView mLeftRecyclerView = (RecyclerView) dialog.findViewById(R.id.recycler_left_time);
            RecyclerView mRightRecyclerView = (RecyclerView) dialog.findViewById(R.id.recycler_right_time);
            RecyclerViewLayoutManager.setLinearVerticalManager(AgainYuYueJiuzhenPersonActivity.this, mLeftRecyclerView);
            RecyclerViewLayoutManager.setLinearVerticalManager(AgainYuYueJiuzhenPersonActivity.this, mRightRecyclerView);
            mLeftTime = serviceTimeBean.getData().get(0).getDay();
            selectTime = serviceTimeBean.getData().get(0).getDay();
            mLeftAdapter = new BaseRecyclerRefreshAdapter<SelectServiceTimeBean.SelectServiceTimeBeanData>(AgainYuYueJiuzhenPersonActivity.this, serviceTimeBean.getData(), R.layout.yuyue_keshi_item) {
                @Override
                public void convert(BaseRecyclerHolder holder, SelectServiceTimeBean.SelectServiceTimeBeanData item, int position, boolean isScrolling) throws IOException {
                    holder.setText(R.id.yuyue_item, item.getDay());
                    TextView mItem = holder.getView(R.id.yuyue_item);
                    if (position == getmPosition()) {
                        mItem.setBackgroundColor(Color.parseColor("#F5F5F5"));
                    } else {
                        mItem.setBackgroundColor(Color.parseColor("#ffffff"));
                    }
                }
            };
            mLeftRecyclerView.setAdapter(mLeftAdapter);
            mRightList = serviceTimeBean.getData().get(0).getPeriods();
            mRightAdapter = new BaseRecyclerRefreshAdapter<SelectServiceTimeBean.SelectServiceTimeBeanData.PeriodsData>(AgainYuYueJiuzhenPersonActivity.this, mRightList, R.layout.yuyue_keshi_item) {
                @Override
                public void convert(BaseRecyclerHolder holder, SelectServiceTimeBean.SelectServiceTimeBeanData.PeriodsData item, int position, boolean isScrolling) throws IOException {
                    holder.setText(R.id.yuyue_item, item.getPeriodDesc());
                    TextView mItem = holder.getView(R.id.yuyue_item);
                    if (position == getmPosition()) {
                        mItem.setBackgroundColor(Color.parseColor("#F5F5F5"));
                    } else {
                        mItem.setBackgroundColor(Color.parseColor("#ffffff"));
                    }
                }
            };
            mRightRecyclerView.setAdapter(mRightAdapter);
            mLeftAdapter.setOnItemClickListener(new BaseRecyclerRefreshAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(RecyclerView parent, View view, int position) {
                    mRightList = serviceTimeBean.getData().get(position).getPeriods();
                    selectTime = serviceTimeBean.getData().get(position).getDay();
                    mLeftTime = serviceTimeBean.getData().get(position).getDay();
                    mRightAdapter = new BaseRecyclerRefreshAdapter<SelectServiceTimeBean.SelectServiceTimeBeanData.PeriodsData>(AgainYuYueJiuzhenPersonActivity.this, mRightList, R.layout.yuyue_keshi_item) {
                        @Override
                        public void convert(BaseRecyclerHolder holder, SelectServiceTimeBean.SelectServiceTimeBeanData.PeriodsData item, int position, boolean isScrolling) throws IOException {
                            holder.setText(R.id.yuyue_item, item.getPeriodDesc());
                            TextView mItem = holder.getView(R.id.yuyue_item);
                            if (position == getmPosition()) {
                                mItem.setBackgroundColor(Color.parseColor("#F5F5F5"));
                            } else {
                                mItem.setBackgroundColor(Color.parseColor("#ffffff"));
                            }
                        }
                    };
                    mRightRecyclerView.setAdapter(mRightAdapter);
                    mRightAdapter.setOnItemClickListener(new BaseRecyclerRefreshAdapter.OnItemClickListener() {
                        @Override
                        public void onItemClick(RecyclerView parent, View view, int position) {
                            mTimeStates = mRightList.get(position).getPeriod();
                            select_shijian.setText(mLeftTime + "日" + mRightList.get(position).getPeriodDesc());
                            dialog.dismiss();
                        }
                    });
                    mLeftAdapter.changeState(position);
                    mLeftAdapter.notifyDataSetChanged();
                }
            });

            mRightAdapter.setOnItemClickListener(new BaseRecyclerRefreshAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(RecyclerView parent, View view, int position) {
                    mTimeStates = mRightList.get(position).getPeriod();
                    select_shijian.setText(mLeftTime + "日" + mRightList.get(position).getPeriodDesc());
                    dialog.dismiss();
                }
            });
        } else {
            ToastUtil.showToastINFO(AgainYuYueJiuzhenPersonActivity.this, serviceTimeBean.getMsg());
        }


    }

    @Override
    public void SetPersenter(Object persenter) {

    }
    @Override
    public void OnShowErrorMsg(String mErrorMsg) {
        mpcd.dismiss();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // TODO: 2022/4/12  requestCode 为100 resultCode为200  处理医院列表返回信息
        if (requestCode == 100 && resultCode == 200) {
            patientId = data.getLongExtra("mJiuPersonId", 0);
            mName = data.getStringExtra("name");
            mCarId = data.getStringExtra("sfzhm");
            mobile = data.getStringExtra("sjhm");
            mJiuzhengPerson.setText(mName + "  " + mobile);
        } else if (requestCode == 300 && resultCode == 400) {
            mHospitalName = data.getStringExtra("yiyuan");
            hospitalId = data.getLongExtra("hospitalId", 0);
            select_yiuan.setText(mHospitalName);
        } else if (requestCode == 500 && resultCode == 600) {
            mKeshiName = data.getStringExtra("keshi");
            select_ke.setText(data.getStringExtra("keshi"));
            keshiId = data.getStringExtra("keshiId");
        }
    }
}
